define(["jquery","bbcare","fu/app/comm/comm",
	"scprit/nutrition/app/departmentScheme/departmentScheme.servers.js",
	"scprit/nutrition/app/departmentScheme/departmentScheme.ui.js"],
	function($,$bb,$comm,$servers,$ui){
	var event = {};

	/**
	 * 新建营养方案成功后的回调函数
	 */
	var ajaxSchemeBasicModifyCallBack = function(data,schemeId,className){
        var str = schemeId ? "添加新" : "修改";
		$bb.toastShow(str + "营养方案成功！");
		$("." + className).modal("hide");
		$servers.ajaxSchemeList(ajaxSchemeListCallBack);
	}

	/**
	 * 点击新建营养方案按钮
	 */
	var onSaveScheme = function(){
        var className = $(this).data("className");
		var params = $servers.getSchemeBasicModifyParams($(this));
		$servers.ajaxSchemeBasicModify(params,ajaxSchemeBasicModifyCallBack,className);
	}

	/**
     * 创建模态框成功后的回调函数
     */
    var onAddScheme = function(){
        var className = "addNutritionSchemeModal";
        $comm.modal({
            "className":className,
            "tit":"添加新方案"
        },function(){
            $ui.addSchemeForm({
                "dom":$("." + className + " .modal-body")
            });
            $("." + className).modal("show");
            $("#saveSchemeBtn").unbind("click");
            $("#saveSchemeBtn").data({"className":className});
            $("#saveSchemeBtn").click(onSaveScheme);
        });
    }

    /**
     * 设置方案
     */
    var onSetScheme = function(){
        var data = $(this).data("data");
        var schemeId = $(this).parents(".scheme-box").attr("schemeId");
        var originalData = $servers.getSetSchemeOriginalData(data,schemeId);
        var className = "setNutritionSchemeModal";
        $comm.modal({
            "className":className,
            "tit":"修改方案"
        },function(){
            $ui.addSchemeForm({
                "dom":$("." + className + " .modal-body"),
                "data":originalData
            });
            $("." + className).modal("show");
            $("#saveSchemeBtn").unbind("click");
            $("#saveSchemeBtn").data({"className":className});
            $("#saveSchemeBtn").click(onSaveScheme);
        });
    }

    /**
     * 点击编辑按钮进入编辑页面
     */
    var onEdit = function(){
        var schemeId = $(this).parent(".scheme-box").attr("schemeId");
        var schemeVersionId = $(this).parent(".scheme-box").attr("schemeVersionId");
        var params = {
            "schemeId":schemeId,
            "schemeVersionId":schemeVersionId
        };
    	$bb.openPage("editScheme",{"values":params});
    }

    /**
     * 方案复制成功后的回调函数
     */
    var schemeCloneCallBack = function(data){
        $servers.ajaxSchemeList(ajaxSchemeListCallBack);
    }

    /**
     * 拷贝方案
     */
    var onCopyScheme = function(){
        var schemeId = $(this).parents(".scheme-box").attr("schemeId");
        $servers.schemeClone(schemeId,schemeCloneCallBack);
    }

    /**
     * 请求营养方案列表成功后的回调函数
     */
    var ajaxSchemeListCallBack = function(data){
        $ui.schemeListInit({
            "id":"departmentScheme",
            "list":data
        });
        $("#departmentScheme .scheme-box").mouseenter(function(){
            $(this).find(".scheme-name i").removeClass("fa-folder-o").addClass("fa-folder-open-o");
            $(this).find(".scheme-operation").slideDown();
        }).mouseleave(function(){
            $(this).find(".scheme-name i").removeClass("fa-folder-open-o").addClass("fa-folder-o");
            $(this).find(".scheme-operation").slideUp();
        });
        // 跳转到营养方案详情页
        $("#departmentScheme .scheme-box .scheme-info").unbind("click");
        $("#departmentScheme .scheme-box .scheme-info").click(onEdit);
        // 添加营养方案
        $("#new-scheme").unbind("click");
        $("#new-scheme").click(onAddScheme);
        // 修改营养方案基本信息
        $("#departmentScheme .scheme-box .setScheme").unbind("click");
        $("#departmentScheme .scheme-box .setScheme").data({"data":data});
        $("#departmentScheme .scheme-box .setScheme").click(onSetScheme);
        // 拷贝方案
        $("#departmentScheme .scheme-box .copyScheme").unbind("click");
        $("#departmentScheme .scheme-box .copyScheme").click(onCopyScheme);
    };
    event.ajaxSchemeListCallBack = function(data){
		ajaxSchemeListCallBack(data);
	}

	return event;
})